﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWTPEG
    {
        public TWTPEG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWTEINZ();
            this.DataContext = MV;
            LMS = "P";
            InitializeComponent(); 
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            string pattern = "";
            string pv = "0";
            TBMain.Clear();
            if (ORDERNUM.Length < 15)
                return;
            dwData = TBMain.NewRow();
            dwData["WTEEINZNR"] = (app.Mandant + ORDERNUM.Trim()).PadRight(23) + "P";
            SResult = TMV.QWTPG(dwData);
            if (SResult.Tables[0].Rows.Count == 0)
                return;          
            int dfs = Convert.ToInt16(SResult.Tables[0].Rows[0]["VALUE"].ToString());
            LOADPATTERN(SResult,dfs);
            TBPG = null;
            TBPG = TBMain; 
        }
      

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                string pattern = "";
                int dfs = Convert.ToInt16(DFS);
                for (int idx = 0; idx < dfs; idx++)
                {
                    string pos = "";
                    pos = Helper.Right("0000" + idx, 4);
                    pattern += row[LMS + pos].ToString();
                }
                row["TXTKTEXT"] = Helper.Left(pattern + "000000000000000000000000000000000000000000000000000000000000000000000000", 72);
                TMV.VTK.UpdateTXTKON(row);
            }
            base.OnSave();
        }
        public override void OnEEZChg()
        {
            if (IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.WTEEZ = EEZ;
            base.OnEEZChg();
        }
        public override void OnDFSChg(string _dfs)
        {
            try
            {
                base.OnDFSChg(_dfs);
                if (!IsEdit)
                    return;
                InitialTable(_dfs);
                TBPG = null;
                TBPG = TBMain;
                return;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
      
        public override void ByteChgCommand(object sender, MouseButtonEventArgs e)
        {
            if (!IsEdit)
                return;
            Image img = sender as Image;
            if (((DataGrid)master).CurrentItem == null)
                return;
            DataRow dwNow = ((DataRowView)((DataGrid)master).CurrentItem).Row;
            if (dwNow == null)
                return;

            dwNow[img.Tag.ToString()] = Math.Abs(Convert.ToInt16(dwNow[img.Tag.ToString()]) - 1);
            PGPS = "-1";
            PGPS = dwNow["LINE"].ToString() + "," + img.Tag.ToString() + "," + dwNow[img.Tag.ToString()].ToString();
            this.Focus();
        }


    }
}
